package org.dromara.house.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.house.domain.bo.ParkTenantBo;
import org.dromara.house.domain.entity.ParkFile;
import org.dromara.house.domain.entity.ParkHousing;
import org.dromara.house.domain.entity.ParkTenant;
import org.dromara.house.mapper.ParkFileMapper;
import org.dromara.house.mapper.ParkTenantMapper;
import org.dromara.house.service.ParkTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 * 园区租户信息主表 服务实现类
 * </p>
 *
 * @author LCH
 * @since 2025-08-26
 */
@Service
public class ParkTenantServiceImpl extends ServiceImpl<ParkTenantMapper, ParkTenant> implements ParkTenantService {
    @Autowired
    private ParkFileMapper parkFileMapper;

    @Override
    public TableDataInfo<ParkTenant> selectPageList(ParkTenantBo bo, PageQuery pageQuery) {
        Page<ParkTenant> page = baseMapper.selectPageList(pageQuery.build(), this.buildQueryWrapper(bo));
        for (ParkTenant record : page.getRecords()) {
            if (ObjectUtil.isNotEmpty(record.getLogoId())) {
                ParkFile parkFile = parkFileMapper.selectById(record.getLogoId());
                record.setParkFile(parkFile);
            }
        }
        return TableDataInfo.build(page);
    }

    @Override
    public ParkTenant selectById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertParkHousing(ParkTenant parkTenant) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        parkTenant.setCreateTime(new Date());
        parkTenant.setCreateBy(loginUser.getUserId());
        parkTenant.setIsDeleted(0);
        return baseMapper.insert(parkTenant);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateParkHousing(ParkTenant parkTenant) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        parkTenant.setUpdateTime(new Date());
        parkTenant.setUpdateBy(loginUser.getUserId());
        return baseMapper.updateById(parkTenant);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Long[] ids) {
        for (Long id : ids) {
            ParkTenant parkTenant = baseMapper.selectById(id);
            parkTenant.setIsDeleted(1);
            baseMapper.updateById(parkTenant);
        }

        return 1;
    }

    private Wrapper<ParkTenant> buildQueryWrapper(ParkTenantBo bo) {
        LambdaQueryWrapper<ParkTenant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(bo.getTenantCode()), ParkTenant::getTenantCode, bo.getTenantCode());
        wrapper.eq(ParkTenant::getIsDeleted,0);
        wrapper.eq(ObjectUtil.isNotEmpty(bo.getPhone()), ParkTenant::getPhone, bo.getPhone());
        wrapper.eq(ObjectUtil.isNotEmpty(bo.getContactPerson()), ParkTenant::getContactPerson, bo.getContactPerson());
        wrapper.like(ObjectUtil.isNotEmpty(bo.getEnterpriseName()), ParkTenant::getEnterpriseName, bo.getEnterpriseName());
        wrapper.orderByDesc(ParkTenant::getCreateTime);
        return wrapper;
    }
}
